import { randomNumber } from '../src/util/random';
/**
 * @description: 模拟网络请求延时
 * @param {*} time
 * @return {*}
 */
const waitTime = (time = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

/**
 * @description: 模拟获取验证码
 * @param {*} req
 * @param {*} res
 * @return {*}
 */
async function getFakeCaptcha(req, res) {
  await waitTime(2000);
  return res.json(randomNumber(4));
}

// const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
// let access = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ? 'admin' : '';

/**
 * 当前用户的权限，如果为空代表没登录
 * current user access， if is '', user need login
 */
// 默认初始用户都没有权限
let access = '';
//获取当前用户权限
const getAccess = () => {
  return access;
};

export default {
  // 支持值为 Object 和 Array
  // 获取当前用户
  'GET /api/currentUser': (req, res) => {
    // 没权限 没登录 强制退出
    if (!getAccess()) {
      res.status(401).send({
        data: {
          isLogin: false,
        },
        errorCode: '401',
        errorMessage: '请先登录！',
        success: true,
      });
      return;
    }
    if (access === 'admin') {
      res.send({
        success: true,
        data: {
          name: 'admin',
          realName: '黄越',
          avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
          userid: '00000001',
          email: 'antdesign@alipay.com',
          signature: '海纳百川，有容乃大',
          profile: '这个账号是Telemedicine平台管理者',
          title: '平台管理方',
          group: 'Telemedicine平台管理者',
          tags: [
            {
              key: '0',
              label: '很有想法的',
            },
          ],
          notifyCount: 12,
          unreadCount: 11,
          country: 'China',
          access: 'admin',
          geographic: {
            province: {
              label: '浙江省',
              key: '330000',
            },
            city: {
              label: '杭州市',
              key: '330100',
            },
          },
          address: '西湖区工专路 77 号',
          phone: '0752-268888888',
        },
      });
    }
    if (access === 'user') {
      res.send({
        success: true,
        data: {
          name: 'user',
          realName: '黄越',
          avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
          userid: '10000001',
          email: 'antdesign@alipay.com',
          profile: '这个账号是Telemedicine平台使用者',
          signature: '海纳百川，有容乃大',
          title: '成都中医药大学附属医院',
          group: '成都中医药大学附属医院',
          tags: [
            {
              key: '0',
              label: '负责',
            },
            {
              key: '1',
              label: '非常棒',
            },
          ],
          notifyCount: 12,
          unreadCount: 11,
          country: 'China',
          access: 'user',
          geographic: {
            province: {
              label: '四川省',
              key: '330000',
            },
            city: {
              label: '成都市',
              key: '330100',
            },
          },
          address: '西湖区工专路 77 号',
          phone: '0752-268888888',
        },
      });
    }
    if (access === 'engineer') {
      res.send({
        success: true,
        data: {
          name: 'engineer',
          realName: '黄越',
          avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
          userid: '20000001',
          email: 'antdesign@alipay.com',
          profile: '这个账号是Telemedicine平台工程师',
          signature: '海纳百川，有容乃大',
          title: '工程师',
          group: '四川省成都市第一人民医院-工程师',
          tags: [
            {
              key: '0',
              label: '负责',
            },
            {
              key: '1',
              label: '非常棒',
            },
          ],
          notifyCount: 12,
          unreadCount: 11,
          country: 'China',
          access: 'engineer',
          geographic: {
            province: {
              label: '四川省',
              key: '330000',
            },
            city: {
              label: '成都市',
              key: '330100',
            },
          },
          address: '西湖区工专路 77 号',
          phone: '0752-268888888',
        },
      });
    }
  },
  // GET POST 可省略
  'GET /api/users': [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
    },
  ],

  /**
   * @description: 登陆逻辑判断
   * @param {*}
   * @return {*}
   */
  'POST /api/login/account': async (req, res) => {
    const { password, username, type } = req.body;

    await waitTime(2000);
    // 账号1 admin
    if (password === 'ant.design' && username === 'admin') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      access = 'admin';
      return;
    }
    // 账号2 user
    if (password === 'ant.design' && username === 'user') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'user',
      });
      access = 'user';
      return;
    }
    //账号3 enginner
    if (password === 'ant.design' && username === 'engineer') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'engineer',
      });
      access = 'engineer';
      return;
    }

    res.send({
      status: 'error',
      type,
      errorMessage: '账号密码不匹配',
      currentAuthority: 'guest',
    });
    access = 'guest';
    return;
  },
  'POST /api/login/phone': async (req, res) => {
    const { mobile, captcha, type } = req.body;
    await waitTime(2000);
    if (captcha === '1234') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      access = 'admin';
      return;
    }
    res.send({
      status: 'error',
      errorMessage: '验证码错误',
      type,
      currentAuthority: 'guest',
    });
    access = 'guest';
    return;
  },

  'POST /api/login/outLogin': (req, res) => {
    access = '';
    res.send({
      data: {},
      success: true,
    });
  },
  'POST /api/register': (req, res) => {
    res.send({
      status: 'ok',
      currentAuthority: 'user',
      success: true,
    });
  },
  'GET /api/500': (req, res) => {
    res.status(500).send({
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
    });
  },
  'GET /api/404': (req, res) => {
    res.status(404).send({
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
    });
  },
  'GET /api/403': (req, res) => {
    res.status(403).send({
      timestamp: 1513932555104,
      status: 403,
      error: 'Forbidden',
      message: 'Forbidden',
      path: '/base/category/list',
    });
  },
  'GET /api/401': (req, res) => {
    res.status(401).send({
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },
  'GET  /api/login/captcha': getFakeCaptcha,
};
